package com.CGFrame.struts.bean.keypath;

import java.util.*;
import java.util.Date;
import java.sql.*;
import com.CGFrame.struts.bean.*;
import com.CGFrame.struts.bean.gant.GantBean;
import com.CGFrame.struts.POJOs.*;

import org.apache.struts.util.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class KeypathBean {
	
	ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
	public boolean Test (Make mk1,Make mk2)
	{
		boolean Flag = true;

		for(Iterator it = mk2.getMakerelationsForMake1id().iterator();it.hasNext();)
		{

			Make temp_mk = ((Makerelation)it.next()).getMakeByMake2id();
			if(Integer.parseInt(temp_mk.getMakeId()+"")==Integer.parseInt(mk1.getMakeId()+""))
			{
				Flag = false;
			}
			else Flag = Test(mk1,temp_mk);
			if(Flag==false)break;
		}
		
		return Flag;
	}//�޻����
	
	

	
	
	public Vector RelationStake(Object ob,String Type)
	{
		Vector vec = new Vector();
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			for(Iterator it = element.getMakes().iterator();it.hasNext();)
			{
				Make mk = (Make)it.next();
				if(!mk.getMakerelationsForMake1id().isEmpty())
				{
					for (Iterator it1 = mk.getMakerelationsForMake1id().iterator();it1.hasNext();)
					{
						vec.add((Makerelation)it1.next());
					}
				}		
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			for(Iterator it = shot.getMakes().iterator();it.hasNext();)
			{
				Make mk = (Make)it.next();
				if(!mk.getMakerelationsForMake1id().isEmpty())
				{
					for (Iterator it1 = mk.getMakerelationsForMake1id().iterator();it1.hasNext();)
					{
						vec.add((Makerelation)it1.next());
					}
				}		
			}
		}
		return vec;
	}//��Ԫ�����������j
	
	public boolean IsIn (Vector vec,int id,String Type)
	{
		boolean bool = false;
		for(int i=0;i<vec.size();i++)
		{
			if(Type.equals("Makerelation"))
			{
				Makerelation mk_rela = (Makerelation)vec.elementAt(i);
				if(Integer.parseInt(mk_rela.getMakeRelationId()+"")==id)
				{
					bool = true;
					break;
				}
			}
			if(Type.equals("Make"))
			{
				Make mk= (Make)vec.elementAt(i);
				if(Integer.parseInt(mk.getMakeId()+"")==id)
				{
					bool = true;
					break;
				}
			}
		}
		return bool;
	}//�ж��Ƿ�ĳһ����������j�Ƿ�����ڼ���Vector֮��
	
	
	
	public Vector MakesAsMake1 (Object ob,String Type)
	{
		Vector vec = new Vector();
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			Vector makes = RelationMakes(element,"Element");
			for(int i=0;i<makes.size();i++)
			{
				Make mk = (Make)makes.elementAt(i);
				if(mk.getMakerelationsForMake2id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			Vector makes = RelationMakes(shot,"Shot");
			for(int i=0;i<makes.size();i++)
			{
				Make mk = (Make)makes.elementAt(i);
				if(mk.getMakerelationsForMake2id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		return vec;
	}//Ԫ������ǰ�������
	public Vector MakesAsMake2 (Object ob,String Type)
	{
		Vector vec = new Vector();
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			Vector makes = RelationMakes(element,"Element");
			for(int i=0;i<makes.size();i++)
			{
				Make mk = (Make)makes.elementAt(i);
				if(mk.getMakerelationsForMake1id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			Vector makes = RelationMakes(shot,"Shot");
			for(int i=0;i<makes.size();i++)
			{
				Make mk = (Make)makes.elementAt(i);
				if(mk.getMakerelationsForMake1id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		return vec;
	}//Ԫ�����޺�̵�����
	
	public Vector NoRelationMakes(Object ob,String Type)
	{
		Vector vec = new Vector();
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			for(Iterator it = element.getMakes().iterator();it.hasNext();)
			{
				Make mk = (Make)it.next();
				if(mk.getMakerelationsForMake1id().isEmpty()&&mk.getMakerelationsForMake2id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			for(Iterator it = shot.getMakes().iterator();it.hasNext();)
			{
				Make mk = (Make)it.next();
				if(mk.getMakerelationsForMake1id().isEmpty()&&mk.getMakerelationsForMake2id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		return vec;
	}//Ԫ���в��漰��j������
	
	public Vector RelationMakes(Object ob, String Type)
	{
		Vector vec = new Vector();
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			for(Iterator it = element.getMakes().iterator();it.hasNext();)
			{
				Make mk = (Make)it.next();
				if((!mk.getMakerelationsForMake1id().isEmpty())||(!mk.getMakerelationsForMake2id().isEmpty()))
				{
					//System.out.println("RelationMakes MakeId ="+mk.getMakeId());
					vec.add(mk);
				}
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			for(Iterator it = shot.getMakes().iterator();it.hasNext();)
			{
				Make mk = (Make)it.next();
				if((!mk.getMakerelationsForMake1id().isEmpty())||(!mk.getMakerelationsForMake2id().isEmpty()))
				{
					//System.out.println("RelationMakes MakeId ="+mk.getMakeId());
					vec.add(mk);
				}
			}
		}
		return vec;
	}//Ԫ�����漰��j������
	
	public Vector Make1s(Object ob,String Type)
	{
		Vector vec = new Vector();
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			Vector makes = RelationMakes(element,"Element");
			for(int i=0;i<makes.size();i++)
			{
				Make mk = (Make)makes.elementAt(i);
				if(!mk.getMakerelationsForMake1id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			Vector makes = RelationMakes(shot,"Shot");
			for(int i=0;i<makes.size();i++)
			{
				Make mk = (Make)makes.elementAt(i);
				if(!mk.getMakerelationsForMake1id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		return vec;
	}
	public EventNode [] InitArray (EventNode [] eve)
	{
		for(int i=0;i<eve.length;i++)
		{
			eve[i] = new EventNode(0,-1,-1,-1,"");
		}
		return eve;
	}//���ں�̵����м���
	
	

	public EventNode [] InitRelation(Object ob,String Type)
	{
		EventNode [] eve = null;
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			Vector makes = RelationMakes(element,"Element");
			if(makes!=null)
			{
				eve =  new EventNode [makes.size()];
				eve = InitArray(eve);
				for(int i=0;i<makes.size();i++)
				{
					Make mk = (Make)makes.elementAt(i);
					eve[i].setId(mk.getMakeId());
					eve[i].setName(mk.getMakeName());
					eve[i].setRest(mk.getExceptHours());
					
					// setTip
					StringBuffer tip = new StringBuffer();
					Iterator tasks = mk.getTasks().iterator();
					while(tasks.hasNext())
					{
						Task task = (Task)tasks.next();
						if (task.getReguser()!=null)
						{
							tip.append(task.getReguser().getRegName()).append("  ");						
						}
					}
					eve[i].setTip(tip.toString());
					///end setTip
					
					//setPercent
					String statusName =mk.getStatus().getStatusName(); 
					eve[i].setPercent( GantBean.getPercentByStatus(statusName));
					//endPercent
					
					if(IsIn(MakesAsMake1(element,"Element"),mk.getMakeId(),"Make"))
					{
						eve[i].setVe(0);
					}
				}
				SetAllVe(eve,element,"Element");
				int total = TotalHours(eve,element,"Element");
				for(int i=0;i<eve.length;i++)
				{
					Make mk = (Make)makes.elementAt(i);
					if(IsIn(MakesAsMake2(element,"Element"),mk.getMakeId(),"Make"))
					{
						eve[i].setVl(total-mk.getExceptHours());
					}
				}
				SetAllVl(eve,element,"Element");
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			Vector makes = RelationMakes(shot,"Shot");
			if(makes!=null)
			{
				eve =  new EventNode [makes.size()];
				eve = InitArray(eve);
				for(int i=0;i<makes.size();i++)
				{
					Make mk = (Make)makes.elementAt(i);
					eve[i].setId(mk.getMakeId());
					eve[i].setName(mk.getMakeName());
					eve[i].setRest(mk.getExceptHours());
					// setTip
					StringBuffer tip = new StringBuffer();
					Iterator tasks = mk.getTasks().iterator();
					while(tasks.hasNext())
					{
						Task task = (Task)tasks.next();
						if (task.getReguser()!=null)
						{
							tip.append(task.getReguser().getRegName()).append("  ");						
						}
					}
					eve[i].setTip(tip.toString());
					///end setTip
					
					//setPercent
					String statusName =mk.getStatus().getStatusName(); 
					eve[i].setPercent( GantBean.getPercentByStatus(statusName));
					//endPercent
					
					
					if(IsIn(MakesAsMake1(shot,"Shot"),mk.getMakeId(),"Make"))
					{
						eve[i].setVe(0);
					}
				}
				SetAllVe(eve,shot,"Shot");
				int total = TotalHours(eve,shot,"Shot");
				for(int i=0;i<eve.length;i++)
				{
					Make mk = (Make)makes.elementAt(i);
					if(IsIn(MakesAsMake2(shot,"Shot"),mk.getMakeId(),"Make"))
					{
						eve[i].setVl(total-mk.getExceptHours());
					}
				}
				SetAllVl(eve,shot,"Shot");
			}
		}
		return eve;
	}//����,���ú󷵻�һ��EventNode���͵�����EventNode���Ͷ����EventNode.java
	
	public void VE (EventNode [] eve,Make mk)
	{
		int max = 0;
		int Flag = 0;
		for(Iterator it = mk.getMakerelationsForMake2id().iterator();it.hasNext();)
		{
			int current = 0;
			int tag;
			Makerelation temp_mkrela = (Makerelation)it.next();
			Make temp_mk = temp_mkrela.getMakeByMake1id();
			tag = FindNode(eve,temp_mk.getMakeId());
			if(eve[tag].getVe()==-1)
			{
				Flag = 1;
				break;
			}
			else
			{
				current = eve[tag].getVe()+temp_mk.getExceptHours();
				if(max<current)max=current;
			}
		}
		if(Flag==0)
		{
			int tag_mk = FindNode(eve,mk.getMakeId());
			eve[tag_mk].setVe(max);
		}
	}//��ĳһ�����VE,������������
	public void SetAllVe (EventNode [] eve,Object ob,String Type)
	{
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			Vector makes = RelationMakes(element,"Element");
			for(;;)
			{
				int Flag=0;
				for(int i=0;i<eve.length;i++)
				{
					if(eve[i].getVe()==-1)
					{
						Flag=1;
						break;
					}
				}
				if(Flag==0)break;
				else
				{
					for(int i=0;i<makes.size();i++)
					{
	
						Make temp_mk = (Make)makes.elementAt(i);
						VE(eve,temp_mk);
					}
				}
				
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			Vector makes = RelationMakes(shot,"Shot");
			for(;;)
			{
				int Flag=0;
				for(int i=0;i<eve.length;i++)
				{
					if(eve[i].getVe()==-1)
					{
						Flag=1;
						break;
					}
				}
				if(Flag==0)break;
				else
				{
					for(int i=0;i<makes.size();i++)
					{
	
						Make temp_mk = (Make)makes.elementAt(i);
						VE(eve,temp_mk);
					}
				}
				
			}
		}
	}//�������е�ǰԪ���µ������VE
	
	public int TotalHours(EventNode [] eve,Object ob,String Type)
	{
		int max = 0;
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			Vector makes = MakesAsMake2(element,"Element");
			for(int i=0;i<makes.size();i++)
			{
				Make mk = (Make)makes.elementAt(i);
				int Flag = FindNode (eve,mk.getMakeId());
				int current = 0;
				current = eve[Flag].getVe()+mk.getExceptHours();
				if(current>max)
					max = current;
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			Vector makes = MakesAsMake2(shot,"Shot");
			for(int i=0;i<makes.size();i++)
			{
				Make mk = (Make)makes.elementAt(i);
				int Flag = FindNode (eve,mk.getMakeId());
				int current = 0;
				current = eve[Flag].getVe()+mk.getExceptHours();
				if(current>max)
					max = current;
			}
		}
		return max;
	}//������ʱ��
	
	public void VL (EventNode [] eve,Make mk)
	{
		if(mk.getShot()==null)
		{
			int min = TotalHours(eve,mk.getElement(),"Element");
			int Flag = 0;
			for(Iterator it = mk.getMakerelationsForMake1id().iterator();it.hasNext();)
			{
				int current = TotalHours(eve,mk.getElement(),"Element");
				int tag;
				Makerelation temp_mkrela = (Makerelation)it.next();
				Make temp_mk = temp_mkrela.getMakeByMake2id();
				tag = FindNode(eve,temp_mk.getMakeId());
				if(eve[tag].getVl()==-1)
				{
					Flag = 1;
					break;
				}
				else
				{
					current = eve[tag].getVl()-mk.getExceptHours();
					if(min>current)min=current;
				}
			}//��ĳһ�����VL,������������
			if(Flag==0)
			{
				int tag_mk = FindNode(eve,mk.getMakeId());
				eve[tag_mk].setVl(min);
			}
		}
		else if(mk.getElement()==null)
		{
			int min = TotalHours(eve,mk.getShot(),"Shot");
			int Flag = 0;
			for(Iterator it = mk.getMakerelationsForMake1id().iterator();it.hasNext();)
			{
				int current = TotalHours(eve,mk.getShot(),"Shot");
				int tag;
				Makerelation temp_mkrela = (Makerelation)it.next();
				Make temp_mk = temp_mkrela.getMakeByMake2id();
				tag = FindNode(eve,temp_mk.getMakeId());
				if(eve[tag].getVl()==-1)
				{
					Flag = 1;
					break;
				}
				else
				{
					current = eve[tag].getVl()-mk.getExceptHours();
					if(min>current)min=current;
				}
			}//��ĳһ�����VL,������������
			if(Flag==0)
			{
				int tag_mk = FindNode(eve,mk.getMakeId());
				eve[tag_mk].setVl(min);
			}
		}
	}
	public void SetAllVl (EventNode [] eve,Object ob,String Type)
	{
		if(Type.equals("Element"))
		{
			Element element = (Element)ob;
			Vector makes = Make1s(element,"Element");
			for(;;)
			{
				int Flag=0;
				for(int i=0;i<eve.length;i++)
				{
					if(eve[i].getVl()==-1)
					{
						Flag=1;
						break;
					}
				}
				if(Flag==0)break;
				else
				{
					for(int i=0;i<makes.size();i++)
					{
						Make temp_mk = (Make)makes.elementAt(i);
						VL(eve,temp_mk);
					}
				}
				
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			Vector makes = Make1s(shot,"Shot");
			for(;;)
			{
				int Flag=0;
				for(int i=0;i<eve.length;i++)
				{
					if(eve[i].getVl()==-1)
					{
						Flag=1;
						break;
					}
				}
				if(Flag==0)break;
				else
				{
					for(int i=0;i<makes.size();i++)
					{
						Make temp_mk = (Make)makes.elementAt(i);
						VL(eve,temp_mk);
					}
				}
				
			}
		}
	}//�������е�ǰԪ���µ������VL
	
	
	
	
	public int FindNode (EventNode [] eve,int parameter)
	{
		int result=0;
		for(int i=0;i<eve.length;i++)
		{
			if(eve[i].getId()==parameter)
				result=i;
		}
		return result;
	}//���������������е�λ��
	
	public Vector NoRelationCatemodels(Object ob,String Type)
	{
		Vector vec = new Vector();
		if(Type.equals("Element"))
		{
			Catemodel catemodel = (Catemodel)ob;
			for(Iterator it = catemodel.getCatemodeltimes().iterator();it.hasNext();)
			{
				Make mk = (Make)it.next();
				if(mk.getMakerelationsForMake1id().isEmpty()&&mk.getMakerelationsForMake2id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		else if(Type.equals("Shot"))
		{
			Shot shot = (Shot)ob;
			for(Iterator it = shot.getMakes().iterator();it.hasNext();)
			{
				Make mk = (Make)it.next();
				if(mk.getMakerelationsForMake1id().isEmpty()&&mk.getMakerelationsForMake2id().isEmpty())
				{
					vec.add(mk);
				}
			}
		}
		return vec;
	}
	
	public EventNode [] InitCate (Object ob,String type)
	{
		Vector vec = NoRelationCatemodels(ob, type);
		EventNode [] eve = InitRelation(ob,type);
		int total_hours = 0;
		if(eve==null||eve.length==0)
		{
			//System.out.println("Test01");
			EventNode [] new_eve = new EventNode [vec.size()];
			new_eve = InitArray(new_eve);
			//System.out.println("Test02");
			int max=0;
			for(int i=0;i<vec.size();i++)
			{
				Make make = (Make)vec.elementAt(i);
				if(make.getExceptHours()>max)
				{
					max=make.getExceptHours();
				}
			}
			//System.out.println("Test03");
			for(int i=0;i<vec.size();i++)
			{
				Make make = (Make)vec.elementAt(i);
				new_eve[i].setId(make.getMakeId());
				new_eve[i].setName(make.getMakeName());
				new_eve[i].setRest(make.getExceptHours());
				new_eve[i].setVe(0);
				new_eve[i].setVl(max-make.getExceptHours());
			}
			return new_eve;
		}
		else
		{

			total_hours = this.TotalHours(eve, ob, type);
			//System.out.println("Test1");
			int minus = 0;
			for(int i=0;i<vec.size();i++)
			{
				Make make = (Make)vec.elementAt(i);
				if(make.getExceptHours()>total_hours&&minus<make.getExceptHours()-total_hours)
				{
					minus = make.getExceptHours()-total_hours;
				}
			}
			//System.out.println("Test2");
			if(minus!=0)
			{
				for(int i=0; i<eve.length;i++)
				{
					eve[i].setVl(eve[i].getVl()+minus);
				}
			}
			EventNode [] new_eve = new EventNode [eve.length+vec.size()];
			new_eve = InitArray(new_eve);
			for(int i=0;i<eve.length;i++)
			{
				new_eve[i]=eve[i];
			}
			for(int i=eve.length;i<eve.length+vec.size();i++)
			{
				Make make = (Make)vec.elementAt(i-eve.length);
				new_eve[i].setId(make.getMakeId());
				new_eve[i].setName(make.getMakeName());
				new_eve[i].setRest(make.getExceptHours());
				new_eve[i].setVe(0);
				new_eve[i].setVl(total_hours+minus-make.getExceptHours());
			}
			return new_eve;
		}

	}//�����漰��j������Ҳ�������ڣ�
	 //��ĳһ���漰��j�����ʱ��������й�j��������ʱ��ʱ����������������Vl�ı仯��

	public EventNode [] Init (Object ob,String type)
	{
		Vector vec = NoRelationMakes(ob, type);
		EventNode [] eve = InitRelation(ob,type);
		int total_hours = 0;
		if(eve==null||eve.length==0)
		{
			//System.out.println("Test01");
			EventNode [] new_eve = new EventNode [vec.size()];
			new_eve = InitArray(new_eve);
			//System.out.println("Test02");
			int max=0;
			for(int i=0;i<vec.size();i++)
			{
				Make make = (Make)vec.elementAt(i);
				if(make.getExceptHours()>max)
				{
					max=make.getExceptHours();
				}
			}
			//System.out.println("Test03");
			for(int i=0;i<vec.size();i++)
			{
				Make make = (Make)vec.elementAt(i);
				new_eve[i].setId(make.getMakeId());
				new_eve[i].setName(make.getMakeName());
				new_eve[i].setRest(make.getExceptHours());
				new_eve[i].setVe(0);
				new_eve[i].setVl(max-make.getExceptHours());
				
				//////setTip
				StringBuffer tip = new StringBuffer();
				Iterator tasks = make.getTasks().iterator();
				while(tasks.hasNext())
				{
					Task task = (Task)tasks.next();
					if (task.getReguser()!=null)
					{
						tip.append(task.getReguser().getRegName()).append("  ");						
					}
				}
				new_eve[i].setTip(tip.toString());
				///end setTip
				
				//setPercent
				String statusName =make.getStatus().getStatusName(); 
				new_eve[i].setPercent( GantBean.getPercentByStatus(statusName));
				
			}
			return new_eve;
		}
		else
		{

			total_hours = this.TotalHours(eve, ob, type);
			//System.out.println("Test1");
			int minus = 0;
			for(int i=0;i<vec.size();i++)
			{
				Make make = (Make)vec.elementAt(i);
				if(make.getExceptHours()>total_hours&&minus<make.getExceptHours()-total_hours)
				{
					minus = make.getExceptHours()-total_hours;
				}
			}
			//System.out.println("Test2");
			if(minus!=0)
			{
				for(int i=0; i<eve.length;i++)
				{
					eve[i].setVl(eve[i].getVl()+minus);
				}
			}
			EventNode [] new_eve = new EventNode [eve.length+vec.size()];
			new_eve = InitArray(new_eve);
			for(int i=0;i<eve.length;i++)
			{
				new_eve[i]=eve[i];
			}
			for(int i=eve.length;i<eve.length+vec.size();i++)
			{
				Make make = (Make)vec.elementAt(i-eve.length);
				new_eve[i].setId(make.getMakeId());
				new_eve[i].setName(make.getMakeName());
				new_eve[i].setRest(make.getExceptHours());
				new_eve[i].setVe(0);
				new_eve[i].setVl(total_hours+minus-make.getExceptHours());
//////			setTip
				StringBuffer tip = new StringBuffer();
				Iterator tasks = make.getTasks().iterator();
				while(tasks.hasNext())
				{
					Task task = (Task)tasks.next();
					if (task.getReguser()!=null)
					{
						tip.append(task.getReguser().getRegName()).append("  ");						
					}
				}
				new_eve[i].setTip(tip.toString());
				///end setTip
				
				//setPercent
				String statusName =make.getStatus().getStatusName(); 
				new_eve[i].setPercent( GantBean.getPercentByStatus(statusName));
			}
			return new_eve;
		}

	}



}
